import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsEmail, IsOptional, IsNumber, Min, IsEnum, IsPhoneNumber, Matches } from 'class-validator';
import { Transform } from 'class-transformer';
import { UserRole, UserStatus, UserLanguage, UserTheme, UserNavigation } from '../enums/user.enum';

export class UserQueryDto {
  @ApiProperty({ required: false, description: '页码', default: 1 })
  @IsNumber()
  @Min(1)
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  page?: number = 1;

  @ApiProperty({ required: false, description: '每页数量', default: 10 })
  @IsNumber()
  @Min(1)
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  limit?: number = 10;
}

export class UserEditDto {
  @ApiProperty({ required: false, description: '用户名' })
  @IsString()
  @IsOptional()
  username?: string;

  @ApiProperty({ required: false, description: '邮箱' })
  @IsEmail()
  @IsOptional()
  email?: string;

  @ApiProperty({ required: false, description: '手机号' })
  @IsOptional()
  @Matches(/^$|^\+?[1-9]\d{1,14}$/, { message: '手机号格式不正确' })
  phone?: string;

  @ApiProperty({ required: false, description: '密码' })
  @IsString()
  @IsOptional()
  password?: string;

  @ApiProperty({ required: false, description: '角色', enum: UserRole })
  @IsEnum(UserRole)
  @IsOptional()
  role?: UserRole;

  @ApiProperty({ required: false, description: '状态', enum: UserStatus })
  @IsEnum(UserStatus)
  @IsOptional()
  status?: UserStatus;

  @ApiProperty({ required: false, description: '头像URL' })
  @IsString()
  @IsOptional()
  avatar?: string;

  @ApiProperty({ required: false, description: '语言', enum: UserLanguage })
  @IsEnum(UserLanguage)
  @IsOptional()
  language?: UserLanguage;

  @ApiProperty({ required: false, description: '主题', enum: UserTheme })
  @IsEnum(UserTheme)
  @IsOptional()
  theme?: UserTheme;

  @ApiProperty({ required: false, description: '导航栏样式', enum: UserNavigation })
  @IsEnum(UserNavigation)
  @IsOptional()
  navigation?: UserNavigation;
}

export class UserInfoDto {
  @ApiProperty({ description: '用户ID' })
  id: number;

  @ApiProperty({ description: '用户名' })
  username: string;

  @ApiProperty({ description: '邮箱' })
  email: string;

  @ApiProperty({ description: '手机号', nullable: true })
  phone: string;

  @ApiProperty({ description: '角色', enum: UserRole })
  role: UserRole;

  @ApiProperty({ description: '状态', enum: UserStatus })
  status: UserStatus;

  @ApiProperty({ description: '头像URL', nullable: true })
  avatar: string;

  @ApiProperty({ description: '语言', enum: UserLanguage })
  language: UserLanguage;

  @ApiProperty({ description: '主题', enum: UserTheme })
  theme: UserTheme;

  @ApiProperty({ description: '导航栏样式', enum: UserNavigation })
  navigation: UserNavigation;

  @ApiProperty({ description: '创建时间' })
  createdAt: Date;

  @ApiProperty({ description: '更新时间' })
  updatedAt: Date;
}

export class UserListResponseDto {
  @ApiProperty({ description: '用户列表' })
  items: UserInfoDto[];

  @ApiProperty({ description: '总数' })
  total: number;

  @ApiProperty({ description: '当前页码' })
  page: number;

  @ApiProperty({ description: '每页数量' })
  limit: number;
} 